package com.loongtech.bi.action.realTime;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.support.chart.*;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@Scope("view")
public class Data1Action {

    private Date date1 = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY));
    private Date date2 = new Date(Time.getBeginningOfDay(Time.currentTime()));
    private Data1 data1;
    private List<Data1s> data1s;
    private ChartUIData data1Chart;

    @PostConstruct
    public void init() {
        search();
    }

    public void search() {
        Random random = new Random();
        int d = Time.getInterval(date1, date2);

        data1 = new Data1();
        data1s = new ArrayList<>();
        for (int i = 0; i <= d; i++) {
            Date dd = Time.getDateByAddNum(date1, i);

            Data1s d1s = new Data1s();
            d1s.setDateStr(Time.getDateStr(dd));
            int num = random.nextInt(100 - 50 + 1) + 50;
            int okNum = random.nextInt(num - num / 3 + 1) + num / 3;
            d1s.setNum(num);
            d1s.setOkNum(okNum);
            d1s.setNotNum(num - okNum);
            d1s.setAvgTime(PageHelper.floatKeepOneNumber((float) ((random.nextInt(10 - 5 + 1) + 10)) * 1.0));
            data1s.add(d1s);

            data1.setNum(data1.getNum() + num);
            data1.setOkNum(data1.getOkNum() + okNum);
            data1.setNotNum(data1.getNum() + num - okNum);
            data1.setAvgTime(PageHelper.floatKeepOneNumber((float) ((random.nextInt(10 - 5 + 1) + 10)) * 1.0));
        }

        data1Chart = new ChartUIData();
        data1Chart.setJsonStr(new ChartTool(data1s, Data1s.class, "").loadData(data1Chart.getJsonType()));

    }

    public class Data1 {
        private int num;
        private int okNum;
        private int notNum;
        private float okRadio;
        private float avgTime;

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public int getOkNum() {
            return okNum;
        }

        public void setOkNum(int okNum) {
            this.okNum = okNum;
        }

        public int getNotNum() {
            return notNum;
        }

        public void setNotNum(int notNum) {
            this.notNum = notNum;
        }

        public float getOkRadio() {
            return okRadio;
        }

        public void setOkRadio(float okRadio) {
            this.okRadio = okRadio;
        }

        public float getAvgTime() {
            return avgTime;
        }

        public void setAvgTime(float avgTime) {
            this.avgTime = avgTime;
        }
    }

    public class Data1s {
        @ChartFlow(name = "小时")
        private String dateStr;
        @ChartSelect(name = "参与人数", key = 1)
        @ChartData1(name = "参与人数", key = 1)
        private int num;
        @ChartData1(name = "完成人数", key = 1)
        private int okNum;
        @ChartData1(name = "未完成人数", key = 1)
        private int notNum;
        private float okRadio;
        private float avgTime;

        public String getDateStr() {
            return dateStr;
        }

        public void setDateStr(String dateStr) {
            this.dateStr = dateStr;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public int getOkNum() {
            return okNum;
        }

        public void setOkNum(int okNum) {
            this.okNum = okNum;
        }

        public int getNotNum() {
            return notNum;
        }

        public void setNotNum(int notNum) {
            this.notNum = notNum;
        }

        public float getOkRadio() {
            return okRadio;
        }

        public void setOkRadio(float okRadio) {
            this.okRadio = okRadio;
        }

        public float getAvgTime() {
            return avgTime;
        }

        public void setAvgTime(float avgTime) {
            this.avgTime = avgTime;
        }
    }

    public Date getDate1() {
        return date1;
    }

    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    public Date getDate2() {
        return date2;
    }

    public void setDate2(Date date2) {
        this.date2 = date2;
    }

    public Data1 getData1() {
        return data1;
    }

    public void setData1(Data1 data1) {
        this.data1 = data1;
    }

    public List<Data1s> getData1s() {
        return data1s;
    }

    public void setData1s(List<Data1s> data1s) {
        this.data1s = data1s;
    }

    public ChartUIData getData1Chart() {
        return data1Chart;
    }

    public void setData1Chart(ChartUIData data1Chart) {
        this.data1Chart = data1Chart;
    }
}
